﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.Connectivity.WebServices;

namespace EffectiveFolderPermissions
{
    public partial class Permissions : Form
    {
        public AccessPermis[] SelectedPermissions = null;
        public PrpgType ApplyToChildren = PrpgType.None;


        public Permissions(AccessPermis[] permis, string userName, string folderName)
        {
            InitializeComponent();

            lblFolder.Text = "Folder : " + folderName;
            lblUser.Text = "User : " + userName; 

            dgvPermissions.Rows.Add("Read", false, false);
            dgvPermissions.Rows.Add("Modify", false, false);
            dgvPermissions.Rows.Add("Delete", false, false);

            foreach (AccessPermis ap in permis)
            {
                if (ap != null)
                {
                    UpdateGrid(ap.Id, ap.Val);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (rbtnAppend.Checked || rbtnNone.Checked || rbtnReplace.Checked)
            {


                AccessPermis[] ap = new AccessPermis[0];
                foreach (DataGridViewRow r in dgvPermissions.Rows)
                {
                    DataGridViewCheckBoxCell cbAllow = r.Cells[1] as DataGridViewCheckBoxCell;
                    DataGridViewCheckBoxCell cbDeny = r.Cells[2] as DataGridViewCheckBoxCell;

                    bool allow = (null != cbAllow && null != cbAllow.Value && true == (bool)cbAllow.Value);
                    bool deny = (null != cbDeny && null != cbDeny.Value && true == (bool)cbDeny.Value);


                    if (allow || deny)
                    {
                        switch (r.Cells[0].Value.ToString())
                        {
                            case "Read":
                                ap = UpdateAccessPermis(1, allow, ap);
                                break;
                            case "Modify":
                                ap = UpdateAccessPermis(2, allow, ap);
                                break;
                            case "Delete":
                                ap = UpdateAccessPermis(3, allow, ap);
                                break;
                        }

                    }

                }
                SelectedPermissions = ap;

                string messageText = "Click OK to confirm the following changes:" + Environment.NewLine + Environment.NewLine;
                messageText += lblUser.Text + Environment.NewLine + Environment.NewLine;
                messageText += lblFolder.Text + Environment.NewLine + Environment.NewLine;


                if (rbtnAppend.Checked)
                {
                    ApplyToChildren = PrpgType.AppendPermissions;
                    messageText += "Append permissions to all child folders";
                }
                else if (rbtnReplace.Checked)
                {
                    messageText += "Replace permissions on all child folders";
                    ApplyToChildren = PrpgType.ReplacePermissions;
                }
                else
                {
                    ApplyToChildren = PrpgType.None;
                    messageText += "Only apply this change to the selected folder.";
                }
                this.Close();




                if (!(MessageBox.Show(messageText, "Confirm Your Changes", MessageBoxButtons.OKCancel) == DialogResult.OK))
                {
                    SelectedPermissions = null;
                    ApplyToChildren = PrpgType.None;
                };


            }
        }

        private AccessPermis[] UpdateAccessPermis(long Id, bool allow, AccessPermis[] ap)
        {
            AccessPermis[] temp = null;
            int lastItem = 0;
            temp = new AccessPermis[ap.Length];
            ap.CopyTo(temp, 0);
            ap = new AccessPermis[ap.Length + 1];
            lastItem = ap.Length - 1;
            temp.CopyTo(ap, 0);
            ap[lastItem] = new AccessPermis();
            ap[lastItem].Id = Id;

            if (allow)
            {
                ap[lastItem].Val = true;
            }
            else
            {
                ap[lastItem].Val = false;
            }

            return ap;

        }


        private void UpdateGrid(long Id, bool Val)
        {
            switch (Val)
            {
                case true:
                    dgvPermissions.Rows[(int)Id-1].Cells[1].Value = true;
                    break;
                case false:
                    dgvPermissions.Rows[(int)Id - 1].Cells[2].Value = true;
                    break;
            }

        }

        //private void dgvPermissions_CellContentClick(object sender, DataGridViewCellEventArgs e)
        //{
        //    dgvPermissions.CommitEdit(e.ColumnIndex);
        //}

        private void dgvPermissions_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1 && e.RowIndex >= 0)
            {
                DataGridViewCheckBoxCell cbDeny = dgvPermissions.Rows[e.RowIndex].Cells[2] as DataGridViewCheckBoxCell;
                bool deny = (null != cbDeny && null != cbDeny.Value && true == (bool)cbDeny.Value);
                if (deny)
                {
                    cbDeny.Value = false;
                }
                dgvPermissions.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
            else if (e.ColumnIndex == 2 && e.RowIndex >= 0)
            {
                DataGridViewCheckBoxCell cbAllow = dgvPermissions.Rows[e.RowIndex].Cells[1] as DataGridViewCheckBoxCell;
                bool allow = (null != cbAllow && null != cbAllow.Value && true == (bool)cbAllow.Value);
                if (allow)
                {
                    cbAllow.Value = false;
                }
                dgvPermissions.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }

            
        }

    }
}
